﻿using System;
using System.Collections.Generic;
using System.Web;

namespace Home.WebUI.Areas.Example.Controllers
{
    public class Test1
    {
        /// <summary>规则:({"rules": {"email": {"required": true,"email": true} } })</summary>
        Dictionary<string, Dictionary<string, object>> Rules = new Dictionary<string, Dictionary<string, object>>();
        
        /// <summary>服务器端错误信息</summary>
        public string ErrStr = "";
        /// <summary>是否通过验证</summary>
        bool Checked1 = true;
        /// <summary>
        /// 验证所有Controllers里调用
        /// </summary>
        /// <returns></returns>
        public bool CheckAll(HttpRequestBase request)
        {
            
            foreach (var item in Rules)//---------------------------------------按name名称循环
            {
                string inputValue = request[item.Key];//输入的值
                foreach (var item1 in item.Value)//-----------------------------name名称中包含的验证
                {
                    switch (item1.Key)
                    {
                        case "required":
                            NotEmpty(inputValue);
                            if (!Checked1) return false;//第一次失败则不向下执行。
                            break;
                        default:
                            break;
                    }
                }
            }
            return Checked1;
        }

        /// <summary>不能为空</summary>
        public bool NotEmpty(string inputValue)
        {
            if (string.IsNullOrWhiteSpace(inputValue))
                AddErrStr("不能为空");
            return false;
        }
        public static Dictionary<string, Dictionary<string, object>> AddNotEmpty(this Dictionary<string, Dictionary<string, object>> rules)
        { 
            
        }
        /// <summary>添加服务端的错误信息</summary>
        private void AddErrStr(string message)
        {
            ErrStr += message;
            Checked1 = Checked1 && false;
        }

        public Test1()
        {
            Dictionary<string, object> valid = new Dictionary<string, object>();
            
        }
        /// <summary>
        /// 用户名
        /// </summary>
        public virtual string UserName { get; set; }
        /// <summary>
        /// 密码
        /// </summary>
        public string PassWord { get; set; }
        /// <summary>
        /// 注册Ip
        /// </summary>
        public string Ip { get; set; }
        /// <summary>
        /// Email
        /// </summary>
        public string Email { get; set; }
        /// <summary>
        /// 手机
        /// </summary>
        public string Phone { get; set; }
        /// <summary>
        /// 创建日期
        /// </summary>
        public DateTime? CreateDate { get; set; }
    }
}